package org.check.UI;

import java.awt.Color;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EtchedBorder;

import org.check.Util.XmlParser;
import org.check.VO.DBConnVO;

import net.miginfocom.swing.MigLayout;
import javax.swing.event.CaretListener;
import javax.swing.event.CaretEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class QuickConfigPanel extends JPanel {

	private JTextField text_confName;
	private JTextField text_poolCnt;
	private JComboBox comboBox_version;
	private JComboBox comboBox_dbType;
	private JTextField text_dbIP;
	private JTextField text_dbName;
	private JTextField text_user;
	private JTextField text_pwd;
	private JTextField text_dbUser;
	private List<DBConnVO> dbConnVOs;
	private int confIndex;

	/**
	 * Create the panel.
	 * @param dbConnVOsIn 
	 * @param exportXlsPanel 
	 * @param dbConnSelectPanel 
	 */
	public QuickConfigPanel(List<DBConnVO> dbConnVOsIn, final ExportXlsPanel exportXlsPanel) {
		dbConnVOs = dbConnVOsIn;
		setToolTipText("快速配置仅用于临时改动，若要保存配置，请进入“管理配置”进行相关操作。");
		setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		setBackground(Color.LIGHT_GRAY);
		setLayout(new MigLayout("", "[::80px,grow][::100px,grow]", "[][21px][][24px][][][][][][grow][grow]"));

		JLabel label_11 = new JLabel("配置名称");
		add(label_11, "cell 0 0,alignx trailing");

		text_confName = new JTextField();
		text_confName.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				exportXlsPanel.setConfigName(text_confName.getText());
				exportXlsPanel.updateText();
			}
		});
		add(text_confName, "cell 1 0,growx");
		text_confName.setColumns(10);

		JLabel label_2 = new JLabel("线程数");
		add(label_2, "cell 0 1,alignx trailing");

		text_poolCnt = new JTextField();
		add(text_poolCnt, "cell 1 1,growx,aligny top");
		text_poolCnt.setColumns(10);

		JLabel label_12 = new JLabel("平台版本");
		add(label_12, "cell 0 2,alignx trailing");

		comboBox_version = new JComboBox();
		comboBox_version.setModel(new DefaultComboBoxModel(new String[] { "SP8", "SP16", "2016Q2", "2016Q4" }));
		add(comboBox_version, "cell 1 2,growx");

		JLabel label_7 = new JLabel("数据库类型");
		add(label_7, "cell 0 3,alignx trailing,aligny center");

		comboBox_dbType = new JComboBox();
		comboBox_dbType.setModel(new DefaultComboBoxModel(new String[] { "Oracle", "SQLServer" }));
		add(comboBox_dbType, "cell 1 3,growx");

		JLabel lblip = new JLabel("数据库IP");
		add(lblip, "cell 0 4,alignx trailing,aligny center");

		text_dbIP = new JTextField();
		add(text_dbIP, "cell 1 4,growx");
		text_dbIP.setColumns(10);

		JLabel label_4 = new JLabel("数据库实例名");
		add(label_4, "cell 0 5,alignx trailing");

		text_dbName = new JTextField();
		add(text_dbName, "cell 1 5,growx");
		text_dbName.setColumns(10);

		JLabel label_8 = new JLabel("数据库用户名");
		add(label_8, "cell 0 6,alignx trailing");

		text_user = new JTextField();
		add(text_user, "cell 1 6,growx");
		text_user.setColumns(10);

		JLabel label_9 = new JLabel("数据库密码");
		add(label_9, "cell 0 7,alignx trailing");

		text_pwd = new JTextField();
		add(text_pwd, "cell 1 7,growx");
		text_pwd.setColumns(10);

		JLabel label_10 = new JLabel("表用户名");
		add(label_10, "cell 0 8,alignx trailing");

		text_dbUser = new JTextField();
		add(text_dbUser, "cell 1 8,growx");
		text_dbUser.setColumns(10);

		JTextPane textPane = new JTextPane();
		textPane.setText("注意：快速配置仅用于临时改动，若要保存配置，请进入“管理配置”进行相关操作。");
		textPane.setBackground(Color.LIGHT_GRAY);
		add(textPane, "cell 0 9 2 1,growx,aligny top");
	}
	
	public DBConnVO loadQuickConfig(int confIndex) {
		return loadQuickConfig(dbConnVOs.get(confIndex));
	}
	
	public DBConnVO loadQuickConfig(DBConnVO currentConfig) {
		String user = text_user.getText();
		String pwd = text_pwd.getText();
		String dbUser = text_dbUser.getText();
		String dbName = text_dbName.getText();
		String dbIP = text_dbIP.getText();
		String version = comboBox_version.getSelectedItem().toString();
		if (comboBox_dbType.getSelectedItem().equals("Oracle")) {
			currentConfig.setDbtype("oracle");
			currentConfig.setDriverType("oracle.jdbc.driver.OracleDriver");
			currentConfig.setUrl("jdbc:oracle:thin:@" + dbIP + ":1521:" + dbName);
		} else if (comboBox_dbType.getSelectedItem().equals("SQLServer")) {
			currentConfig.setDbtype("mssql");
			currentConfig.setDriverType("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			currentConfig.setUrl("jdbc:sqlserver://" + dbIP + ":1433;databaseName=" + dbName);
		}
		currentConfig.setUser(user);
		currentConfig.setPassword(pwd);
		currentConfig.setDBuser(dbUser);
		currentConfig.setVersion(version);
		return currentConfig;
	}

	public void repaintQuickConfig(int confIndex) {
		if (confIndex > -1 && null != dbConnVOs.get(confIndex)) {
			text_confName.setText(XmlParser.unicodeDecode(dbConnVOs.get(confIndex).getName()));
			text_user.setText(dbConnVOs.get(confIndex).getUser());
			text_dbUser.setText(dbConnVOs.get(confIndex).getDBuser());
			text_pwd.setText(dbConnVOs.get(confIndex).getPassword());
			text_poolCnt.setText(dbConnVOs.get(confIndex).getPoolCnt());
			comboBox_version.setSelectedItem(dbConnVOs.get(confIndex).getVersion());
			String dbType = dbConnVOs.get(confIndex).getDbtype();
			String url = dbConnVOs.get(confIndex).getUrl();
			// if (dbConnVOs.get(i).getIsRoute()) {
			// tableModel.removeRow(0);
			// for (Server server : dbConnVOs.get(i).getServers()) {
			// Vector<String> emptyVector = new Vector<String>();
			// emptyVector.addElement(server.getHost());
			// emptyVector.addElement(server.getPort().toString());
			// emptyVector.addElement(server.getUser());
			// emptyVector.addElement(server.getPwd());
			// tableModel.addRow(emptyVector);
			// table.setModel(tableModel);
			// }
			// }
			if (dbType.equals("oracle")) {
				comboBox_dbType.setSelectedItem("Oracle");
				String[] urlSplit = url.split(":");
				text_dbName.setText(urlSplit[urlSplit.length - 1]);
				text_dbIP.setText(urlSplit[urlSplit.length - 3].substring(1));
			} else if (dbType.equals("mssql")) {
				comboBox_dbType.setSelectedItem("SQLServer");
				String[] urlSplit = url.split("=");
				text_dbName.setText(urlSplit[urlSplit.length - 1]);
				String[] urlSplit2 = url.split(":");
				text_dbIP.setText(urlSplit2[urlSplit2.length - 2].substring(2));

			} else if (dbType.equals("")) {
				text_dbName.setText("");
				text_dbIP.setText("");
			} else {
				text_dbName.setText("unknown");
				text_dbIP.setText("unknown");
			}
			this.setConfIndex(confIndex);
			repaint();
		}
	}

	public int getConfIndex() {
		return confIndex;
	}

	public void setConfIndex(int confIndex) {
		this.confIndex = confIndex;
	}

}
